package com.evil.Mail;

import com.evil.Utils.PropertiesUtils;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

/********************************************
 * 工程名：Web
 * 包名：com.evil.Mail
 * 时间：16-9-21 下午4:53
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
public class SendMail
{
    public void sendText(MailBean bean) throws MessagingException
    {
        // 配置发送邮件的环境属性
        Properties props = new Properties();

        // 发件人的账号
        //props.put("mail.user", "evilbai@sina.com");
        props.put("mail.user", bean.getFromAddr());
        // 访问SMTP服务时需要提供的密码
        //props.put("mail.password", "evil1714698245");
        props.put("mail.password", bean.getUserPassword());

        props.put("mail.smtp.host", "smtp.sina.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", 25);/*端口默认25*/
        props.put("mail.smtp.starttls.enable", "true");

        // 构建授权信息，用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator()
        {
            @Override
            protected PasswordAuthentication getPasswordAuthentication()
            {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息，创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form = new InternetAddress(
                props.getProperty("mail.user"));
        message.setFrom(form);

        // 设置收件人
        //InternetAddress to = new InternetAddress("evilbai@163.com");
        InternetAddress to = new InternetAddress(bean.getToAddr());

        message.setRecipient(Message.RecipientType.TO, to);

        // 设置邮件标题
        //message.setSubject("测试邮件");
        message.setSubject(bean.getTitle());

        // 设置邮件的内容体
        //message.setContent("<a href='http://www.fkjava.org'>测试的HTML邮件</a>", "text/html;charset=UTF-8");

        message.setContent(
                bean.getContent()
                , "text/html;charset=UTF-8");

        // 发送邮件
        Transport.send(message);
    }

    public void sendFile(MailBean bean) throws MessagingException, UnsupportedEncodingException
    {
        // 配置发送邮件的环境属性
        Properties props = new Properties();

        // 发件人的账号
        //props.put("mail.user", "evilbai@sina.com");
        props.put("mail.user", bean.getFromAddr());
        // 访问SMTP服务时需要提供的密码
        //props.put("mail.password", "evil1714698245");
        props.put("mail.password", bean.getUserPassword());

        //props.put("mail.smtp.host", "smtp.sina.com");
        props.put("mail.smtp.host", "smtp.sina.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", 25);/*端口默认25*/
        props.put("mail.smtp.starttls.enable", "true");

        // 构建授权信息，用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator()
        {
            @Override
            protected PasswordAuthentication getPasswordAuthentication()
            {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息，创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form = new InternetAddress(
                props.getProperty("mail.user"));
        message.setFrom(form);

        // 设置收件人
        //InternetAddress to = new InternetAddress("evilbai@163.com");
        InternetAddress to = new InternetAddress(bean.getToAddr());

        message.setRecipient(Message.RecipientType.TO, to);

        // 设置邮件标题
        //message.setSubject("测试邮件");
        message.setSubject(bean.getTitle());

        // 向multipart对象中添加邮件的各个部分内容，包括文本内容和附件
        Multipart multipart = new MimeMultipart();

        // 添加邮件正文
        BodyPart contentPart = new MimeBodyPart();
        /*设置内容*/
        contentPart.setContent(bean.getContent(), "text/html;charset=UTF-8");
        multipart.addBodyPart(contentPart);

        // 添加附件的内容
        if (bean.getFile() != null)
        {
            BodyPart attachmentBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(bean.getFile());
            attachmentBodyPart.setDataHandler(new DataHandler(source));
            //attachmentBodyPart.setFileName(MimeUtility.encodeWord(bean.getFile().getName()));
            attachmentBodyPart.setFileName(bean.getFile().getName());
            multipart.addBodyPart(attachmentBodyPart);
        }

        // 将multipart对象放到message中
        message.setContent(multipart);
        // 保存邮件
        message.saveChanges();

        // 发送邮件
        Transport.send(message);
    }
}
